package com.kws.tool.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kws.tool.dto.BizContent;
import com.kws.tool.dto.InvoiceItem;
import com.kws.tool.dto.InvoiceRequest;
import com.kws.tool.service.InvoiceService;
import com.kws.tool.util.SignatureUtil;
import com.kws.tool.vo.InvoiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : zhangbenting
 * @date : 2024/11/11 19:22
 * @Version: 1.0
 */
@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Value("${invoice.url}")
    private String invoiceUrl;

    @Value("${invoice.svcId}")
    private String svcId;

    @Value("${invoice.secretKey}")
    private String secretKey;

    @Value("${invoice.signType}")
    private String signType;

    @Value("${invoice.version}")
    private String version;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public InvoiceVo generateInvoice(BizContent bizContent) {

        InvoiceRequest invoiceRequest = new InvoiceRequest();
        try {
            // 设置业务请求参数
            String kpddh = bizContent.getKpddh();
            List<InvoiceItem> invoiceItems = new ArrayList<>();
            InvoiceItem invoiceItem = new InvoiceItem();
            invoiceItem.setSe("");
            invoiceItem.setSl("");
            invoiceItems.add(invoiceItem);
            bizContent.setFyxm(invoiceItems);


            //获取用户订单信息
            bizContent.setDdsj(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            invoiceRequest.setBizContent(bizContent);
            // 设置公共请求参数
            invoiceRequest.setSvcId(svcId);
            invoiceRequest.setSignType(signType);
            invoiceRequest.setTimestamp(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            invoiceRequest.setVersion(version);

            // 生成签名
            Map<String, String> paramMap = invoiceRequest.toMap();
            String signContent = SignatureUtil.createSignatureContent(paramMap);
            String sign = SignatureUtil.sign(signContent, secretKey, "SHA256withRSA");
            invoiceRequest.setSign(sign);

            // 将请求对象转换为 JSON
            String requestJson = objectMapper.writeValueAsString(invoiceRequest);

            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 发送请求
            HttpEntity<String> requestEntity = new HttpEntity<>(requestJson, headers);
            return restTemplate.exchange(
                    invoiceUrl,
                    HttpMethod.POST,
                    requestEntity,
                    InvoiceVo.class
            ).getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
